package com.dayaokeji.rhythmschoolstudent.wiget

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v4.app.DialogFragment
import android.support.v4.app.FragmentTransaction
import android.support.v4.view.ViewCompat
import android.support.v7.app.AppCompatDialogFragment
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.server_api.domain.ExamQuestion
import java.io.Serializable

/**
 *********************************************************************
 **  Created by Zeno on 2018/7/20
 *********************************************************************
 **  version 1.0
 *********************************************************************
 **  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 *********************************************************************
 ** 题目列表
 *********************************************************************
 */
class SubjectListDialog : AppCompatDialogFragment() {

    companion object {
        private const val SUBEJCT_LIST = "subject_list"
        private const val SUBJECT_POSITION = "subject_position"
        fun newInstance(subjects: List<ExamQuestion>): SubjectListDialog {
            val bundle = Bundle()
            bundle.putSerializable(SUBEJCT_LIST,subjects as Serializable)
            val subjectListDialog = SubjectListDialog()
            subjectListDialog.arguments = bundle
            return subjectListDialog
        }
    }

    private lateinit var rvSubjects: RecyclerView
    private var itemClickListener: ItemClickListener? = null
    private var list: List<ExamQuestion>? = null
    private var currentPosition: Int = 0
    private val subjectsAdapter: SubjectsAdapter by lazy { SubjectsAdapter() }
    private lateinit var rootView:View
    private lateinit var llSubjectNum: LinearLayout

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog.window.setBackgroundDrawable(ColorDrawable(Color.parseColor("#70000000")))
        setStyle(DialogFragment.STYLE_NO_FRAME,0)
        rootView = inflater.inflate(R.layout.dialog_subject_list, container, false)
        llSubjectNum = rootView.findViewById(R.id.ll_subject_num_content)
        rvSubjects = rootView.findViewById(R.id.rv_subjects)
        rootView.minimumWidth = activity?.resources?.displayMetrics?.widthPixels!!
        rootView.minimumHeight = activity?.resources?.displayMetrics?.heightPixels!!
        val layoutParams = llSubjectNum.layoutParams
        layoutParams.height = (activity?.resources?.displayMetrics?.heightPixels!! * 0.4).toInt()
        llSubjectNum.layoutParams = layoutParams
        llSubjectNum.translationY = (activity?.resources?.displayMetrics?.heightPixels!! * 0.4).toFloat()
        rootView.findViewById<ImageView>(R.id.iv_close_dialog).setOnClickListener {
            if (dialog.isShowing) {
                dialog.dismiss()
            }
        }
        rootView.setOnClickListener{
            if (dialog.isShowing) {
                dismiss()
            }
        }
        return rootView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        list = arguments?.getSerializable(SUBEJCT_LIST) as List<ExamQuestion>
        subjectsAdapter.setNewData(list)
        rvSubjects.layoutManager = GridLayoutManager(activity,10)
        rvSubjects.adapter = subjectsAdapter
        setCurrentPosition(currentPosition)
        subjectsAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { baseQuickAdapter: BaseQuickAdapter<Any, BaseViewHolder>, view: View, i: Int ->
            itemClickListener?.let {
                val question = baseQuickAdapter.data[i] as ExamQuestion
                list?.let {
                    for (examQuestion in it) {
                        examQuestion.isActive = false
                    }
                }

                question.isActive = true
                baseQuickAdapter.notifyDataSetChanged()
                currentPosition = i
                it.click(question,i)

            }
        }

        ViewCompat.animate(llSubjectNum).translationY(llSubjectNum.height.toFloat()).translationXBy(-llSubjectNum.height.toFloat()).setDuration(500).start()
    }


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        val attributes = dialog.window.attributes
        attributes.gravity = Gravity.BOTTOM
        dialog.setCanceledOnTouchOutside(true)
        dialog.onWindowAttributesChanged(attributes)
        return dialog
    }


    fun setItemClickListener(clickListener: ItemClickListener) {
        this.itemClickListener = clickListener
    }

    fun setCurrentPosition(position: Int) {
        this.currentPosition = position
        list?.let {
            for (examQuestion in it) {
                examQuestion.isActive = false
            }
            if (position<it.size){
                it[position].isActive = true
                subjectsAdapter.notifyDataSetChanged()
            }
        }
    }

    class SubjectsAdapter : BaseQuickAdapter<ExamQuestion,BaseViewHolder>(R.layout.item_dialog_subjects_num,null) {
        override fun convert(helper: BaseViewHolder?, item: ExamQuestion?) {
            item?.let {
                val position = helper?.layoutPosition!! + 1
                val tvSubjectNum = helper?.getView<TextView>(R.id.rb_subject_num)
                tvSubjectNum.isActivated = it.isActive
                tvSubjectNum.text = position.toString()
            }
        }
    }


    interface ItemClickListener {
        fun click(subject: ExamQuestion,position: Int)
    }
}